/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.netbuilder.dal.services.cable;

import com.google.code.netbuilder.dal.entities.Cable;
import com.google.code.netbuilder.dal.entities.Wire;
import com.google.code.netbuilder.dal.providers.ICableProvider;
import com.google.code.netbuilder.dal.providers.jdbc.CableProviderJDBC;
import com.google.code.netbuilder.dal.providers.jpa.CableProviderJPA;
import com.google.code.netbuilder.logging.NetBuilderLogger;
import com.google.code.netbuilder.logging.NetBuilderLoggerFactory;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author marksist
 */

@ManagedBean(name = "createCableService")
@SessionScoped
public class CreateCableService{
    private static final NetBuilderLogger LOGGER = NetBuilderLoggerFactory.getNetBuilderLogger(CreateCableService.class);
    
    private Cable currentCable;

    private Cable resultCable;
    
    private int countWires;
    
    @EJB
    private ICableProvider cp;
    
   
    
    private List<String> types;
    
    public CreateCableService(){
        
        currentCable = new Cable();
        resultCable = new Cable();
        types = new ArrayList<String>();
        types.add("Optical");
        types.add("Copper");
    }
    
    public Cable getCurrentCable() {
        return currentCable;
    }

    public Cable getResultCable() {
        return resultCable;
    }

    public int getCountWires() {
        return countWires;
    }

    public List<String> getTypes() {
        return types;
    }

    public void setCurrentCable(Cable currentCable) {
        this.currentCable = currentCable;
    }

    public void setResultCable(Cable resultCable) {
        this.resultCable = resultCable;
    }

    public void setCountWires(int countWires) {
        this.countWires = countWires;
    }

    public void setTypes(List<String> types) {
        this.types = types;
    }
    
    public void createCable(){
        List<Wire> wires = new ArrayList<Wire>();
        if (countWires <=0){
            LOGGER.debug("Cable didn't create");
            addMessage("Cable didn't create");
            return;
        }
        for (int i = 0; i< countWires; i++){
            Wire wire = new Wire();
            wire.setName(currentCable.getName()+"_wire_"+i);
            wire.setObjectState(0);
            wires.add(wire);
        }
        
        resultCable.setName(currentCable.getName());
        resultCable.setType(currentCable.getType());
        resultCable.setCableWires(wires);
        
        try{
            cp.save(resultCable);
        }catch (RuntimeException e){
            LOGGER.debug(e.getMessage());
            addMessage("Cable didn't create");
            return;
        }
        
        addMessage("Cable "+resultCable.getName()+" create with id="+resultCable.getId());
    }
    
    public void addMessage(String summary) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    }
    
}
